package com.zivwu.library.dao.net.interceptor;

import android.content.SharedPreferences;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;


public class CookieManger implements Interceptor {
    //todo 省略了initCookies、save、clear、reset、add等操作cookie map的方法
    protected Map<String,String> cookies = new HashMap<>(3);

    private SharedPreferences sp;
    public CookieManger(SharedPreferences sp){
        this.sp = sp;
        String cookieString = sp.getString("cookies","");
//        initCookies(cookieString);

    }
    public String toString(){
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : cookies.entrySet()) {
            sb.append(entry.getKey()+'='+entry.getValue()+";");
        }
        return sb.toString();
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request req = chain.request();
        req=req.newBuilder()
                .addHeader("Cookie",this.toString())
                .build();
        Response response = chain.proceed(req);
        return response;
    }
}
